package com.example.administrator.myapplication;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    final  String[] sex={"男","女","不知道"};
    private String cities[]={"广州","厦门","东莞"};
    String aihao[]={"篮球","跳舞","睡觉","打代码"};
    final  boolean ischeack[]=new boolean[6];

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view1= LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_2,null);
            final  EditText name=findViewById(R.id.name);
               final  EditText age=findViewById(R.id.age);
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请输入姓名年龄")
                        .setView(view1)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                try {
                                    String name1 = name.getText().toString();
                                    int age1 = Integer.parseInt(age.getText().toString());
                                    Toast.makeText(MainActivity.this, "你的姓名是" + name1 + ",你的年龄是" + age1, Toast.LENGTH_SHORT).show();
                                    ;
                                }
                                catch (Exception e){
                                    Toast.makeText(MainActivity.this,"你未输入任何值",Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .create()
                        .show();
            }

        });

        //
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("你的爱好是")
                        .setIcon(R.mipmap.ic_launcher)
                        .setMultiChoiceItems(aihao, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                                ischeack[i]=b;
                                if(b){
                                    Toast.makeText(MainActivity.this,"您添加了"+aihao[i],Toast.LENGTH_SHORT).show();
                                }
                                else{Toast.makeText(MainActivity.this,"你取消了"+aihao[i],Toast.LENGTH_SHORT).show();}
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                        StringBuffer buffer=new StringBuffer();
                                for(int j=0;j<ischeack.length;j++){
                            if(ischeack[j]){
                                buffer.append(aihao[j]);
                            }
                        }
                                Toast.makeText(MainActivity.this,"您的爱好有"+buffer,Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
        //
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择你的性别")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"您的性别是"+sex[i],Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }

        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShowRioddialog();
            }
//
            private void ShowRioddialog() {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择你的城市")
                        .setItems(cities, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(MainActivity.this,"你选择的城市是"+cities[i],Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
  //
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShowSimpledialog();
            }

            private void ShowSimpledialog() {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("提示")
                        .setMessage("今天是你的生日")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .create()
                        .show();
            }

            });
        //
    }
}
